package com.lenovo.smarttraffic.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.api.VolleyCallBack;
import com.lenovo.smarttraffic.bean.GsonBean.SenseBean;
import com.lenovo.smarttraffic.bean.LitepalBean.SenseDBBean;
import com.lenovo.smarttraffic.ui.adapter.MyListAdapter;
import com.lenovo.smarttraffic.util.VolleyUtil;
import com.lenovo.smarttraffic.view.RefreshListView;

import org.json.JSONObject;
import org.litepal.LitePal;
import org.litepal.crud.callback.SaveCallback;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Caleb.F
 * on 2020-03-21
 * at 13:11
 */
public class LogRecordActivity extends BaseActivity {
    private RefreshListView mMyListView;
    private MyListAdapter myListAdapter;
    private SimpleDateFormat mSimpleDateFormat;
    private Timer mTimer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSimpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(1);
            }
        }, 0, 2000);
        initView();
        mMyListView.setRefreshListener(new RefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                List<SenseDBBean> all = LitePal.findAll(SenseDBBean.class);
                int count = myListAdapter.getCount();
                switch (all.size() - count) {
                    case 0:
                    case 1:
                        Toast.makeText(LogRecordActivity.this, "正在请求数据，请等待", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                    default:
                        myListAdapter.refreshData(all.subList(0, count + 2));
                        Toast.makeText(LogRecordActivity.this, "已为您添加两组数据", Toast.LENGTH_SHORT).show();
                        break;

                }

                //必须加上延时才能执行
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mMyListView.onRefreshComplete();
                    }
                },1000);

            }

        });
    }

        private void getData () {
            VolleyUtil.post("GetAllSense.do", new JSONObject(), new VolleyCallBack() {
                @Override
                public void onSucceess(JSONObject jsonObject, String response) {
                    SenseBean senseBean = new Gson().fromJson(response, SenseBean.class);
                    if (senseBean.getRESULT().equals("S")) {
                        SenseDBBean senseDBBean = new SenseDBBean(senseBean.getLightIntensity(), senseBean.get_$Pm25270(), senseBean.getCo2(),
                                senseBean.getHumidity(), senseBean.getTemperature(),
                                mSimpleDateFormat.format(new Date()));
                        senseDBBean.save();
                    }
                }

                @Override
                public void onFailure(String onError) {

                }
            });
        }

        private void initView () {
            mMyListView = findViewById(R.id.my_list_view);
            initToolBar("日志分析");
            List<SenseDBBean> all = LitePal.findAll(SenseDBBean.class);
            if (all.size() != 0) {
                LitePal.deleteAll(SenseDBBean.class);
            }
            all.clear();
            myListAdapter = new MyListAdapter(this, all);
            mMyListView.setAdapter(myListAdapter);
        }

        Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        getData();
                        break;
                    default:
                        break;
                }
                return false;

            }
        });


        @Override
        protected int getLayout () {
            return R.layout.activity_log_record;
        }
    }
